package com.example.demo.loader;

import cn.hutool.core.io.resource.ResourceUtil;

import java.awt.*;
import java.io.InputStream;

public class FontLoader {

    private static final String DEFAULT_FONT_PATH = "font/simhei.ttf";

    public static Font loadFont(float size) {
        return loadFont(null, size);
    }

    public static Font loadFont(String fontPath, float size) {
        if (fontPath == null || fontPath.isEmpty()) {
            fontPath = DEFAULT_FONT_PATH;
        }
        try (InputStream is = ResourceUtil.getStreamSafe(fontPath)) {
            if (is == null) {
                throw new RuntimeException("Font not found: " + fontPath);
            }
            Font font = Font.createFont(Font.TRUETYPE_FONT, is);
            return font.deriveFont(size);
        } catch (Exception ex) {
            throw new RuntimeException("Font loading failed: " + fontPath, ex);
        }
    }

}
